package cn.wolfcode.cms.web.controller;

import cn.wolfcode.cms.annotation.PermissionName;
import cn.wolfcode.cms.domain.Role;
import cn.wolfcode.cms.query.QueryObject;
import cn.wolfcode.cms.service.IRoleService;
import cn.wolfcode.cms.util.JSONResult;
import cn.wolfcode.cms.util.PageResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;


    //贴上shiro授权标签, value指定权限表达式, logical表示与或者或
    //Logical.OR 表示只需要满足一个
    //Logical.AND 表示必须全部满足
    //@RequiresPermissions(value={"role:index", "role:list"}, logical = Logical.OR)
    //@RequiresPermissions(value={"role:index", "角色首页"}, logical = Logical.OR)
    @PermissionName("角色首页")
    @RequiresPermissions("role:index")
    @RequestMapping("")
    public String main(){
        return "role/list";
    }

    @PermissionName("角色列表")
    @RequiresPermissions("role:list")
    @RequestMapping("/list")
    @ResponseBody
    public Object list(QueryObject qo){
        PageResult result = null;
        try {
            result = roleService.query(qo);

        }catch (Exception e){
            e.printStackTrace();

        }
        return result;
    }


    @RequestMapping("listData")
    @ResponseBody
    public Object listData(){
        return roleService.selectAll();
    }


    //获取指定id员工对应角色id集合
    @RequestMapping("getRoleIdsByEmployeeId")
    @ResponseBody
    public Object getRoleIdsByEmployeeId(Long id){
        return roleService.getRoleIdsByEmployeeId(id);
    }





    //角色编辑
    @RequiresPermissions("role:edit")
    @PermissionName("角色编辑")
    @RequestMapping("edit")
    @ResponseBody
    public Object edit(Role entity){
        JSONResult result = new JSONResult();
        try {
            roleService.update(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("编辑失败");
        }
        return result ;
    }


    @RequiresPermissions("role:save")
    @PermissionName("角色添加")
    @RequestMapping("save")
    @ResponseBody
    public Object save(Role entity){
        JSONResult result = new JSONResult();
        try {
            roleService.save(entity);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("添加失败");
        }
        return result ;
    }

    @PermissionName("角色删除")
    @RequiresPermissions("role:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(Long id){
        JSONResult result = new JSONResult();
        try {
            roleService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

}
